# -*- coding: utf-8 -*-
from django.shortcuts import render

# Create your views here.
from app_exam_online.models import *
from app_exam_online.forms import *
from django.http import HttpResponse, HttpResponseRedirect, StreamingHttpResponse
from django.shortcuts import render_to_response
from django.views.decorators.csrf import csrf_exempt
from django import forms

def ShowExamPaper(request):
	questions = SingleProblem.objects.filter(ExamID = request.session['ExamID']).order_by("-SingleProblemID")
	Questions =[]
	for question in questions:
		Question = {}
		Question['QuestionTitle'] = question.SingleTitle
		Question['OptionA'] = question.OptionA
		Question['OptionB'] = question.OptionB
		Question['OptionC'] = question.OptionC
		Question['OptionD'] = question.OptionD
		Question['Answer'] = question.SingleAnswer
		print Question
		Questions.append(Question)
	print Questions
	context ={
			'CourseID':ExamPaperInfo.objects.get(ExamID = request.session['ExamID']).CourseID,
			'ExamID':request.session['ExamID'],
			'questions':Questions,
	}
	return  render_to_response('ShowExamPaper.html',context)

class ExamPaperForm(forms.Form):
	coursename = forms.CharField(required=True,
								max_length =20,
								error_messages={'required':'课程名称不能为空','max_length':'课程名称最多20位'})
	examname = forms.CharField(required=True,
							max_length = 40,
							error_messages={'required':'试卷名称不能为空','max_length':'试卷名称最多40位'})

@csrf_exempt
def AddExamPaper(request):
	if request.POST:
		objPost = ExamPaperForm(request.POST)
		ret = objPost.is_valid()
		if ret:
			coursename = objPost.cleaned_data['coursename']
			examname = objPost.cleaned_data['examname']
			ExamPaperInfo(CourseID=coursename,ExamID=examname,ExamTime=90,TotalScore=100,TeacherID=1).save()
			request.session['ExamID'] = examname
			return HttpResponseRedirect('/app_exam_online/AddSingleProblem/')
		else:
			pass
		return render(request,'AddExamPaper.html',{'obj1':objPost})
	else:
			objGet = ExamPaperForm()
			return render(request,'AddExamPaper.html',{'obj1':objGet})

class SingleProblemForm(forms.Form):
	singletitle = forms.CharField(required=True,
								widget = forms.Textarea,
								max_length = 2000,
								error_messages={'required':'题目不能为空','max_length':'题目长度最多2000位'})
	optionA = forms.CharField(required = True,
							max_length = 500,
							error_messages={'required':'选项不能为空','max_length':'选项长度最多500位'})
	optionB = forms.CharField(required = True,
							max_length = 500,
							error_messages={'required':'选项不能为空','max_length':'选项长度最多500位'})
	optionC = forms.CharField(required = True,
							max_length = 500,
							error_messages={'required':'选项不能为空','max_length':'选项长度最多500位'})
	optionD = forms.CharField(required = True,
							max_length = 500,
							error_messages={'required':'选项不能为空','max_length':'选项长度最多500位'})
	SingleChoice = (('A', 'A'), ('B', 'B'),('C','C'),('D','D'))
	singleanswer = forms.ChoiceField(required = True,
							choices = SingleChoice,
							error_messages={'required':'答案不能为空'})

@csrf_exempt
def AddSingleProblem(request):
	if request.POST:
		objPost = SingleProblemForm(request.POST)
		ret = objPost.is_valid()
		if ret:
			print request.POST
			singletitle = objPost.cleaned_data['singletitle']
			optionA = objPost.cleaned_data['optionA']
			optionB = objPost.cleaned_data['optionB']
			optionC = objPost.cleaned_data['optionC']
			optionD = objPost.cleaned_data['optionD']
			singleanswer = objPost.cleaned_data['singleanswer']
			SingleProblem(SingleProblemID = len(SingleProblem.objects.all()) + 1,CourseID = ExamPaperInfo.objects.get(ExamID = request.session['ExamID']).CourseID,ExamID = request.session['ExamID'],SingleTitle = singletitle,OptionA = optionA,OptionB = optionB,OptionC = optionC,OptionD = optionD,SingleAnswer = singleanswer).save()
			if request.POST['submit'] == u'下一题':
				return HttpResponseRedirect('/app_exam_online/AddSingleProblem/')
			else:
				return HttpResponseRedirect('/app_exam_online/ShowExamPaper/')
		else:
			return render(request,'AddSingleProblem.html',{'obj1':objPost})
	else:
		objGet = SingleProblemForm()
		return render(request,'AddSingleProblem.html',{'obj1':objGet})

